package com.jason.core.config.jwt;

import com.jason.entity.SysRole;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.List;

@Data
public class JwtUser implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String password;

    private List<SysRole> roles;

    private List<GrantedAuthority> authorities;

    public JwtUser(){

    }

    public JwtUser(String username, String password, List<GrantedAuthority> authorities){
        this.username = username;
        this.password = password;
//        this.roles = roles;
        this.authorities = authorities;
    }

//    /**
//     * 返回分配给用户的角色列表
//     *
//     * @return
//     */
//    @Override
//    public Collection<? extends GrantedAuthority> getAuthorities() {
//        List<SimpleGrantedAuthority> authList = new ArrayList<>();
//        List<SysRole> roles = this.getRoles();
//        for(SysRole role : roles){
//            authList.add(new SimpleGrantedAuthority(role.getRoleName()));
//        }
//        return authList;
//    }


   /* *//**
     * 返回分配给用户的角色列表
     *
     * @return
     *//*
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authList = new ArrayList<>();
        List<SysRole> roles = this.getRoles();
        if (null != roles){
            for(SysRole role : roles){
                authList.add(new SimpleGrantedAuthority(role.getRoleName()));
            }
        }

        return authList;
    }*/

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    /**
     * 账户是否未过期
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否未锁定
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否未过期
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否激活
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
